import * as actions from '../actions/index';
import utils from 'utils';

const navKey = utils.getUrlSearchParam('navKey') || 'mine';

// 对页面prop 数据进行管理
export const initialState = {
  groupList: [],
  search: '',
  isLoading: true,
  total: 0,
  currentPage: 1,
  navKey,
  order_by: 'updated_at',
  subGroupMap: {},
  group_creator_role: [],
};
const defaultAction = {
  type: 'doNothing',
};

export default function index(state = initialState, action = defaultAction) {
  const newSubGroup = state.subGroupMap;
  switch (action.type) {
    case actions.UPDATE_DATA:
      return { ...state, ...action.data };
    case actions.SET_NAVKEY:
      return { ...state, navKey: action.data };
    case actions.RESET_DATA:
      return { ...initialState };
    case actions.RECEIVE_GROUP_SUBLIST:
      return { ...state, subGroupMap: { ...state.subGroupMap, ...action.data } };
    case actions.UPDATE_GROUP_SUBLIST:
      delete newSubGroup[action.data];
      return { ...state, subGroupMap: { ...newSubGroup } };
    case actions.RECEIVE_GROUP_SETTING:
      return {
        ...state,
        group_creator_role: action.data.group_creator_role,
      };
    default:
      return state;
  }
}
